/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vladimirkolarevic.eventplanner.tapestry.pages;

import com.vladimirkolarevic.eventplanner.Performer;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.UserBean;
import com.vladimirkolarevic.eventplanner.service.MembershipsService;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

/**
 * 
 * @author Vlada
 */
public class Login {

	@ApplicationState
	private User user;

	@ApplicationState
	private List<Performer> performers;

	@Inject
	private MembershipsService membership;

	@InjectPage
	private PleaseRegister pleseRegister;

	@InjectPage
	private AddEvent addEventPage;

	@InjectPage
	private AllEvents allEvents;

	@InjectPage
	private Index index;

	@InjectPage
	private EventInfo eventInfo;

	@Property
	@Persist
	private User userInput;

	@InjectPage
	private UserInfo userInfo;

	@SetupRender
	public void createObject() {
		userInput = new UserBean();
	}

	Object onSuccess() {

		User registeredUser = membership.getUser(userInput);
		if (registeredUser != null) {

			user = registeredUser;
			performers = new LinkedList<Performer>();
			return userInfo;
		}

		return pleseRegister;

	}
}
